package com.inspur.cloud.service.dataspace;

import com.inspur.cloud.entity.dataspace.*;
import com.inspur.cloud.enums.RoleEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduledService {

    @Autowired
    private UserManagerService userManagerService;

    @Autowired
    private UserResRelationSerivce userResRelationSerivce;

    @Autowired
    private HdfsManagerService hdfsManagerService;

    @Autowired
    private HdfsResourceService hdfsResourceService;

    @Autowired
    private HbaseManagerService hbaseManagerService;

    @Autowired
    private HbaseResourceService hbaseResourceService;

    @Autowired
    private HiveManagerService hiveManagerService;

    @Autowired
    private HiveResourceService hiveResourceService;

    @Autowired
    private YarnManagerService yarnManagerService;

    @Autowired
    private YarnResourceService yarnResourceService;

    @Autowired
    private KafkaManagerService kafkaManagerService;

    @Autowired
    private KafkaResourceService kafkaResourceService;

    public void deleteUnusedData() {
        deleteUnusedUser();
        deleteUnusedHdfsResource();
        deleteUnusedHiveResource();
        deleteUnusedHbaseResource();
        deleteUnusedYarnResource();
        deleteUnusedKafkaResource();
//        deleteUnusedRelation();
    }

    private void deleteUnusedHdfsResource() {
        List<HdfsResourceEntity> hdfsList = hdfsResourceService.findAll();
        for (HdfsResourceEntity hdfs : hdfsList) {
            if (hdfs.getStatus().equals(0)) {
                continue;
            }
            List<UserResourceRelationEntity> relationList = userResRelationSerivce.findByResourceIdAndResourceType(hdfs.getId(), "HDFS");
            if (relationList == null || relationList.size() < 1) {
                try {
                    hdfsManagerService.delRes(hdfs.getId(), 0);
                } catch (Exception e) {

                }
            }
        }
    }

    private void deleteUnusedHbaseResource() {
        List<HbaseResourceEntity> hbaseList = hbaseResourceService.findAll();
        for (HbaseResourceEntity hbase : hbaseList) {
            if (hbase.getStatus().equals(0)) {
                continue;
            }
            List<UserResourceRelationEntity> relationList = userResRelationSerivce.findByResourceIdAndResourceType(hbase.getId(), "HBASE");
            if (relationList == null || relationList.size() < 1) {
                try {
                    hdfsManagerService.delRes(hbase.getId(), 0);
                } catch (Exception e) {

                }
            }
        }
    }

    private void deleteUnusedHiveResource() {
        List<HiveResourceEntity> hiveList = hiveResourceService.findAll();
        for (HiveResourceEntity hive : hiveList) {
            if (hive.getStatus().equals(0)) {
                continue;
            }
            List<UserResourceRelationEntity> relationList = userResRelationSerivce.findByResourceIdAndResourceType(hive.getId(), "HIVE");
            if (relationList == null || relationList.size() < 1) {
                try {
                    hdfsManagerService.delRes(hive.getId(), 0);
                } catch (Exception e) {

                }
            }
        }
    }

    private void deleteUnusedKafkaResource() {
        List<KafkaResourceEntity> kafkaList = kafkaResourceService.findAll();
        for (KafkaResourceEntity kafka : kafkaList) {
            if (kafka.getStatus().equals(0)) {
                continue;
            }
            List<UserResourceRelationEntity> relationList = userResRelationSerivce.findByResourceIdAndResourceType(kafka.getId(), "KAFKA");
            if (relationList == null || relationList.size() < 1) {
                try {
                    hdfsManagerService.delRes(kafka.getId(), 0);
                } catch (Exception e) {

                }
            }
        }
    }

    private void deleteUnusedYarnResource() {
        List<YarnResourceEntity> yarnList = yarnResourceService.findAll();
        for (YarnResourceEntity yarn : yarnList) {
            if (yarn.getStatus().equals(0)) {
                continue;
            }
            List<UserResourceRelationEntity> relationList = userResRelationSerivce.findByResourceIdAndResourceType(yarn.getId(), "YARN");
            if (relationList == null || relationList.size() < 1) {
                try {
                    hdfsManagerService.delRes(yarn.getId(), 0);
                } catch (Exception e) {

                }
            }
        }
    }
    private void deleteUnusedUser() {
        List<UserEntity> userList = userManagerService.findAll();
        for (UserEntity user : userList) {
            if (!user.getRole().equals(RoleEnum.User.getValue())) {
                continue;
            }
            if (userManagerService.findUserById(user.getParentId()) == null) {
                try {
                    userManagerService.deleteUserById(user.getId());
                } catch (Exception e) {

                }
            }
        }
    }
}
